package com.hanxu.mallcoupon.service.impl;

import com.hanxu.common.to.MemberPrice;
import com.hanxu.common.to.SkuReductionTo;
import com.hanxu.mallcoupon.dao.MemberPriceDao;
import com.hanxu.mallcoupon.dao.SkuLadderDao;
import com.hanxu.mallcoupon.entity.MemberPriceEntity;
import com.hanxu.mallcoupon.entity.SkuLadderEntity;
import com.hanxu.mallcoupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hanxu.common.utils.PageUtils;
import com.hanxu.common.utils.Query;

import com.hanxu.mallcoupon.dao.SkuFullReductionDao;
import com.hanxu.mallcoupon.entity.SkuFullReductionEntity;
import com.hanxu.mallcoupon.service.SkuFullReductionService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
    @Autowired
    SkuLadderDao skuLadderDao;
    @Autowired
    MemberPriceDao memberPriceDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuReduction(SkuReductionTo skuReductionTo) {
        // 保存满减打折，会员价
        // sms_sku_ladder
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setSkuId(skuReductionTo.getSkuId());
        skuLadderEntity.setFullCount(skuReductionTo.getFullCount());
        skuLadderEntity.setDiscount(skuReductionTo.getDiscount());
        skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
        if (skuLadderEntity.getFullCount() > 0) {
            skuLadderDao.insert(skuLadderEntity);
        }

        // sms_sku_full_reduction
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuReductionTo, skuFullReductionEntity);
        if (skuFullReductionEntity.getFullPrice().compareTo(new BigDecimal(0)) > 0) {
            this.save(skuFullReductionEntity);

        }

        // sms_member_price
        List<MemberPrice> memberPrice = skuReductionTo.getMemberPrice();
        if (memberPrice != null && !memberPrice.isEmpty()) {
            for (MemberPrice price : memberPrice) {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
                memberPriceEntity.setMemberLevelId(price.getId());
                memberPriceEntity.setMemberLevelName(price.getName());
                memberPriceEntity.setMemberPrice(price.getPrice());
                memberPriceEntity.setAddOther(1);
                if(memberPriceEntity.getMemberPrice().compareTo(BigDecimal.ZERO) > 0){
                    memberPriceDao.insert(memberPriceEntity);
                }
            }
        }

    }

}